package com.ytjj.qmyx.admin.annotation.oplog;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.annotation.Id;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 操作日志
 * 使用说明：
 * 只需要在controller的方法中加上：OperationLogger.log("xxx"); 即可
 */
@Slf4j
@Configuration
public class OperationProductLogger {

    //    private final static ThreadLocal<List<String>> OPERACTION_LOGGER_CONTEXT = new ThreadLocal<List<String>>();
    private final static ThreadLocal<List<Map<String, Object>>> OPERACTION_LOGGER_CONTEXT = new ThreadLocal<List<Map<String, Object>>>();

    private final static int REMARK_LENGTH = 4000;

    /**
     * 记录操作日志明细
     *
     * @param message
     */
    public static void log(Map<String, Object> message) {
        List<Map<String, Object>> data = OPERACTION_LOGGER_CONTEXT.get();
        if (data == null) {
            data = new ArrayList<>();
            OPERACTION_LOGGER_CONTEXT.set(data);
        }
        data.add(message);
    }

    /**
     * 获取当前线程变量的日志内容
     *
     * @return
     */
    protected static List<Map<String, Object>> getRemarkMsg() {
        List<Map<String, Object>> data = OPERACTION_LOGGER_CONTEXT.get();
        if (data != null) {
            StringBuffer buffer = new StringBuffer();
            data.forEach(s -> buffer.append(s.get("msg")).append("\n"));
            return data;
        }
        return null;
    }

    /**
     * 获取当前线程变量中的商品id
     *
     * @return
     */
    protected static Integer getProductId() {
        List<Map<String, Object>> data = OPERACTION_LOGGER_CONTEXT.get();
        if (data != null) {
            List<Integer> ids = new  ArrayList<>();
            for (Map<String, Object> datum : data) {
                Object obj = datum.get("id");
                if(null == obj){
                    continue;
                }
                Integer id = (Integer) obj;
                ids.add(id);
            }
//            StringBuffer buffer = new StringBuffer();
//            data.forEach(s -> buffer.append(s.get("id")));
            return ids.get(0);
        }
        return null;
    }
    /**
     * 获取当前线程变量中的批量商品id
     *
     * @return
     */
    protected static List<Integer> getAllProductId() {
        List<Map<String, Object>> data = OPERACTION_LOGGER_CONTEXT.get();
        if (data != null) {
            Map<String, Object> stringObjectMap = data.get(0);
            Object ids = stringObjectMap.get("id");
            List<Integer> result = new ArrayList<>();
            if (ids instanceof ArrayList<?>) {
                for (Object o : (List<?>) ids) {
                    result.add(Integer.class.cast(o));
                }
            }
            return result;
        }
        return null;
    }


    /**
     * 移除本地变量
     */
    public static void clear() {
        OPERACTION_LOGGER_CONTEXT.remove();
    }

}
